#!/bin/sh -e

# build
#	Simple wiringPi build and install script
#
#	Copyright (c) 2012-2024 Gordon Henderson and contributors
#################################################################################
# This file is part of wiringPi:
#	A "wiring" library for the Raspberry Pi
#
#    wiringPi is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    wiringPi is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
#################################################################################
#
# wiringPi is designed to run on a Raspberry Pi only.
#	However if you're clever enough to actually look at this script to
#	see why it's not building for you, then good luck.
#
#	To everyone else: Stop using cheap alternatives. Support the
#	Raspberry Pi Foundation	as they're the only ones putting money
#	back into education!
#################################################################################

sudo=${WIRINGPI_SUDO-sudo}
make="make -j $(( $(nproc) + 1 ))"
hardware=$(fgrep -a Hardware /proc/cpuinfo | head -1 | awk '{ printf("%s %s %s\n", $3, $4, $5) }' | xargs)
[ "$hardware",, != *"bananapi"* ] \
  && [ -f "/sys/firmware/devicetree/base/model" ] \
  && hardware=$(cat /sys/firmware/devicetree/base/model)

check_make_ok() {
  if [ $? != 0 ]; then
    echo ""
    echo "Make Failed..."
    echo "Please check the messages and fix any problems. If you're still stuck,"
    echo "then raise a GitHub issue with the output and as many details as you can"
    echo "  https://github.com/WiringPi/WiringPi/issues"
    echo ""
    exit 1
  fi
}

configure_gpiomem() {
  [ $(env | grep DEB_BUILD | wc -l) -ne 0 ] && return

  GPIOMEM="/dev/gpiomem"

  if [ -z $1 ] && [ "$(stat -c "%a %G" "$GPIOMEM")" != "660"*"pi" ]; then
    echo "Configure /dev/gpiomem"
    case "$(echo $hardware | tr [:upper:] [:lower:])" in
      *m5|*m2s|*cm4|*cm5io|*cm5-bpicm4io)
        $sudo cp -f udev/rules.d/99-wiringpi-amlogic.rules /etc/udev/rules.d/
        ;;
      *m4berry|*m4zero|*f5)
        $sudo cp -f udev/rules.d/99-wiringpi-sunxi.rules /etc/udev/rules.d/
        ;;
      *f3)
        $sudo cp -f udev/rules.d/99-wiringpi-spacemit.rules /etc/udev/rules.d/
        ;;
      *)
        echo "This system seems not Bananapi"
        ;;
    esac

    echo "Reload udev..."
    [ -x "$(command -v udevadm)" ] \
      && $sudo udevadm trigger \
      || echo "udevadm not found. Please reboot to take effect"
  elif [ "$1" = "uninstall" ]; then
    echo "Deconfigure /dev/gpiomem"
    $sudo rm -f /etc/udev/rules.d/99-wiringpi-*
  else
    echo "Not found $GPIOMEM"
    echo "You will not be able to use WiringPi without root permission"
  fi
}

install() {
  echo "wiringPi Build script"
  echo "====================="
  echo

  echo
  echo "WiringPi Library"
  cd wiringPi
  if [ x$1 = "xstatic" ]; then
    $make static
    check_make_ok
    $sudo $make install-static
  else
    $make
    check_make_ok
    $sudo $make install
  fi
  check_make_ok

  echo
  echo "WiringPi Devices Library"
  cd ../devLib
  $sudo $make uninstall
  if [ x$1 = "xstatic" ]; then
    $make static
    check_make_ok
    $sudo $make install-static
  else
    $make
    check_make_ok
    $sudo $make install
  fi
  check_make_ok

  echo
  echo "WiringPi Devices Library"
  cd ../devLib
  if [ x$1 = "xstatic" ]; then
    $make static
    check_make_ok
    $sudo $make install-static
  else
    $make
    check_make_ok
    $sudo $make install
  fi
  check_make_ok

  echo
  echo "GPIO Utility"
  cd ../gpio
  $make
  check_make_ok
  $sudo $make install
  check_make_ok

  echo
  cd ..
  configure_gpiomem

  echo
  echo All Done.
  echo ""
  echo "NOTE: To compile programs with wiringPi, you need to add:"
  echo "    -lwiringPi -wiringPiDev"
  echo "  to your compile line(s) To use the Gertboard, MaxDetect, etc."
  echo "  code (the devLib), you need to also add:"
  echo "    -lwiringPiDev"
  echo "  to your compile line(s)."
  echo ""
}

uninstall() {
  cd wiringPi
  echo -n "wiringPi: " ; $sudo $make uninstall
  cd ../devLib
  echo -n "DevLib:   " ; $sudo $make uninstall
  cd ../gpio
  echo -n "gpio:     " ; $sudo $make uninstall
  cd ..
  configure_gpiomem uninstall
  echo
}

clean() {
  cd wiringPi
  echo -n "wiringPi:   "	; $make clean
  cd ../devLib
  echo -n "DevLib:     "	; $make clean
  cd ../gpio
  echo -n "gpio:       "	; $make clean
  cd ../examples
  echo -n "Examples:   "	; $make clean
  cd Gertboard
  echo -n "Gertboard:  "	; $make clean
  cd ../PiFace
  echo -n "PiFace:     "	; $make clean
  cd ../q2w
  echo -n "Quick2Wire: "	; $make clean
  cd ../PiGlow
  echo -n "PiGlow:     "	; $make clean
  cd ../scrollPhat
  echo -n "scrollPhat: "	; $make clean
  cd ../..
  echo -n "Deb: " ; rm  -f debian-template/wiringpi*.deb
  echo
}

if [ x$1 = "xclean" ]; then
  clean
  exit
fi

if [ x$1 = "xuninstall" ]; then
  uninstall
  exit
fi

# Only if you know what you're doing!

if [ x$1 = "xdebian" ]; then
  vMaj=`cut -d. -f1 VERSION`
  vMin=`cut -d. -f2 VERSION`
  here=`pwd`
  deb_destdir=${here}/debian-template/wiringPi
  cd debian-template/wiringPi
  export VERSION=$vMaj.$vMin
  export ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)
  echo version:$VERSION architecture:$ARCH
  envsubst < control_template > DEBIAN/control
  rm -rf usr
  cd $here/wiringPi
  make install-deb DEB_DESTDIR=${deb_destdir}
  cd $here/devLib
  make install-deb INCLUDE='-I. -I../wiringPi' DEB_DESTDIR=${deb_destdir}
  cd $here/gpio
  make install-deb INCLUDE='-I../wiringPi -I../devLib' LDFLAGS=-L../debian-template/wiringPi/usr/lib DEB_DESTDIR=${deb_destdir}
  cd $here/debian-template
  fakeroot dpkg-deb -Zxz --build wiringPi
  dpkg-name -o wiringPi.deb
  exit
fi

if [ x$1 != "x" ]; then
  echo "Usage: $0 [clean | uninstall | debian]"
  exit 1
fi

uninstall; clean; install
